package com.zy.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zy.entity.Customer;
import com.zy.entity.User;
import com.zy.entity.Visit;
import com.zy.service.LinkManService;
import com.zy.service.UserService;
import com.zy.service.VisitService;
import com.zy.service.impl.LinkManServiceImpl;
import com.zy.service.impl.UserServiceImpl;
import com.zy.service.impl.VisitServiceImpl;

/**
 * Servlet implementation class VisitServlet
 */
@WebServlet("/VisitServlet")
public class VisitServlet extends HttpServlet {
	VisitService vs = new VisitServiceImpl();
	LinkManService lk = new LinkManServiceImpl();
	UserService us = new UserServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String control = request.getParameter("control");
		if (control.equals("listAll")) {
			List<Visit> visits = vs.queryall();
			request.setAttribute("list", visits);
			request.getRequestDispatcher("/WEB-INF/visit/list.jsp").forward(request, response);
		}
		if (control.equals("toUpdateVisit")) {
			
			int vid = Integer.parseInt(request.getParameter("id"));
			
			Visit visit = vs.queryById(vid);
			request.setAttribute("visit", visit);
			
			List<Customer> customers = lk.getCustomer();
			request.setAttribute("listCustomer", customers);
			
			List<User> users = us.queryAll();
			request.setAttribute("listUser", users);
			
			request.getRequestDispatcher("/WEB-INF/visit/edit.jsp").forward(request, response);
			
		}
		if (control.equals("updateVisit")) {
			Visit visit = new Visit();
			visit.setVid(Integer.parseInt(request.getParameter("vid")));
			visit.setVaddress(request.getParameter("vaddress"));
			visit.getCustomer().setCid(Integer.parseInt(request.getParameter("cvid")));
			visit.getUser().setUid(Integer.parseInt(request.getParameter("uvid")));
			visit.setVcontent(request.getParameter("vcontent"));
			vs.update(visit);
			
			response.sendRedirect(request.getContextPath()+"/VisitServlet?control=listAll");
		}
		
		if(control.equals("queryByAddress")) {
			String address = request.getParameter("vaddress");
			List<Visit> visits = vs.queryByAddress(address);
			request.setAttribute("list", visits);
			request.getRequestDispatcher("/WEB-INF/visit/list.jsp").forward(request, response);
		}
		
		if(control.equals("toAddVisit")) {
			List<Customer> customers = lk.getCustomer();
			List<User> users = us.queryAll();
			request.setAttribute("cList", customers);
			request.setAttribute("uList", users);
			request.getRequestDispatcher("/WEB-INF/visit/add.jsp").forward(request, response);
			
			
		}
		if (control.equals("addVisit")) {
			Visit visit = new Visit();
			visit.setVaddress(request.getParameter("vaddress"));
			visit.getCustomer().setCid(Integer.parseInt(request.getParameter("cvid")));
			visit.getUser().setUid(Integer.parseInt(request.getParameter("uvid")));
			visit.setVcontent(request.getParameter("vcontent"));
			
			vs.insert(visit);
			response.sendRedirect(request.getContextPath()+"/VisitServlet?control=listAll");
		}
		if (control.equals("toDynamicQuery")) {
			List<Customer> customers = lk.getCustomer();
			request.setAttribute("listCustomer", customers);
			
			List<User> users = us.queryAll();
			request.setAttribute("listUser", users);
			
			request.getRequestDispatcher("/WEB-INF/visit/select.jsp").forward(request, response);
		}
		if (control.equals("dynamicQuery")) {
			int cvid = Integer.parseInt(request.getParameter("cvid"));
			int uvid = Integer.parseInt(request.getParameter("uvid"));
			String vaddress = request.getParameter("vaddress");
			List<Visit> visits = new ArrayList<>();
			if (vaddress.length()==0) {
				//仅通过cvid 和uvid查询
				visits = vs.queryByCU(cvid, uvid);
			}
			if (vaddress.length()!=0) {
				//仅通过cvid 和uvid 和 vaddress查询
				visits = vs.queryByCUA(cvid, uvid, vaddress);
			}
			request.setAttribute("list", visits);
			request.getRequestDispatcher("/WEB-INF/visit/list.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
